﻿using DDD.Application.Servieces;
using DDD.Application.Servieces.Dtos.MinIODtos;
using DDD.Common.Const;
using DDD.Core.Result;
using Microsoft.AspNetCore.Mvc;

namespace DDD.Api.Controllers
{
    [ApiExplorerSettings(GroupName = SwaggerConst.Group.MINIO, IgnoreApi = false)]
    [Route("api/[controller]")]
    [ApiController]
    public class MinIOController : ControllerBase
    {
        private readonly MinIOService _minIOService;

        public MinIOController(MinIOService minIOService)
        {
            _minIOService = minIOService;
        }

        /// <summary>
        /// 创建存储桶 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost("bucket")]
        public async Task<ApiResult> CreateBucket(string name)
        {
            await _minIOService.CreateBucket(name);
            return ApiResult.Success();
        }

        /// <summary>
        /// 获取所有的桶
        /// </summary>
        /// <returns></returns>
        [HttpGet("bucket/all")]
        public async Task<ApiResult<IEnumerable<string>>> GetAllBuckets()
        {
            var result = await _minIOService.GetAllBuckets();
            return ApiResult<IEnumerable<string>>.Success(result);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("upload/file")]
        public async Task<ApiResult<string>> UploadFile(UploadFileInput input)
        {
            var result = await _minIOService.UploadFile(input);
            return ApiResult<string>.Success(result);
        }

        /// <summary>
        /// 上传文件 以流的形式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("upload/stream")]
        public async Task<ApiResult<string>> UploadFileSteam(UploadFileSteamInput input)
        {
            var result = await _minIOService.UploadFileSteam(input);
            return ApiResult<string>.Success(result);
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("download")]
        public async Task<FileResult> DownloadFile(DownloadFileInput input)
        {
            var result = await _minIOService.DownloadFile(input);
            return File(result, "application/octet-stream", input.FileName);
        }

        /// <summary>
        /// 获取所有的文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <returns></returns>
        [HttpGet("files/{bucket:required}")]
        public async Task<ApiResult<IEnumerable<string>>> GetAllFiles(string bucket)
        {
            var result = await _minIOService.GetAllFiles(bucket);
            return ApiResult<IEnumerable<string>>.Success(result);
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("file/copy")]
        public async Task<ApiResult> CopyFile(CopyFileInput input)
        {
            await _minIOService.CopyFile(input);
            return ApiResult.Success();
        }

        /// <summary>
        /// 获取桶的信息
        /// </summary>
        /// <param name="bucket"></param>
        /// <returns></returns>
        [HttpGet("bucket/{bucket:required}")]
        public async Task<ApiResult<GetBcuketInfoOutput>> GetBcuketInfo(string bucket)
        {
            var result = await _minIOService.GetBcuketInfo(bucket);
            return ApiResult<GetBcuketInfoOutput>.Success(result);
        }

        /// <summary>
        /// 移除桶
        /// </summary>
        /// <param name="bucket"></param>
        /// <returns></returns>
        [HttpDelete("{bucket:required}")]
        public async Task<ApiResult> RemoveBucket(string bucket)
        {
            await _minIOService.RemoveBucket(bucket);
            return ApiResult.Success();
        }

        /// <summary>
        /// 生成一个预览文件的Uri
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("flile/pre")]
        public async Task<ApiResult<string>> CreatePreFileUri(CreatePreFileUriInput input)
        {
            var result = await _minIOService.CreatePreFileUri(input);
            return ApiResult<string>.Success(result);
        }
    }
}
